package com.tsingteng.cosfun.utils;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import com.tsingteng.cosfun.bean.AttentFansbean;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KeywordUtil {
    /**
     * 关键字高亮变色
     *
     * @param color   变化的色值
     * @param text    文字
     * @param keyword 文字中的关键字
     * @return
     */
    public static SpannableString matcherAtTxt(int color, String text,
                                               String keyword) {
        SpannableString s = new SpannableString(text);
        Pattern p = Pattern.compile(keyword);
        Matcher m = p.matcher(s);
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            s.setSpan(new ForegroundColorSpan(color), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return s;
    }

    /**
     * 多个关键字高亮变色
     *
     * @param color   变化的色值
     * @param text    文字
     * @param keyword 文字中的关键字数组
     * @return
     */
    public static SpannableString matcherAtTxt(int color, String text,
                                               List<AttentFansbean.Data> keyword) {
        SpannableString s = new SpannableString(text);
        for (int i = 0; i < keyword.size(); i++) {
            Pattern p = Pattern.compile("@" + keyword.get(i).nickName);
            Matcher m = p.matcher(s);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                s.setSpan(new ForegroundColorSpan(color), start, end,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return s;
    }


    public static void matchTxt() {
        String aa = "<nickName@看车老大爷&profileId@100017%cosfun>哈哈哈哈<nickName@烤饭1&profileId@7%cosfun>啦啦啦啦";
        SpannableString spannableString = new SpannableString(aa);
        //定义正则表达式
        final String AT = "<nickName.*?cosfun>";// @人
        //设置正则
        Pattern pattern = Pattern.compile(AT);
        Matcher matcher = pattern.matcher(spannableString);

        while (matcher.find()) {
            // 根据group的括号索引，可得出具体匹配哪个正则(0代表全部，1代表第一个括号)
            final String at = matcher.group(0);
            // 处理@符号
            if (at != null) {
                Log.e("matchtxt", "at=" + at);
            }
        }


    }
}
